package leecode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author csp 2021-04-25
 */
public class Question17 {
    public static void main(String[] args) {
        Question17 question17 = new Question17();
        System.out.println(question17.letterCombinations("23"));
    }

    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0) {
            return new ArrayList<>();
        }
        char[][] chars = {
                {'a', 'b', 'c'},
                {'d', 'e', 'f'},
                {'g', 'h', 'i'},
                {'j', 'k', 'l'},
                {'m', 'n', 'o'},
                {'p', 'q', 'r', 's'},
                {'t', 'v', 'u'},
                {'w', 'x', 'y', 'z'}
        };

        String[] array = {""};
        for (int i = 0; i < digits.length(); i++) {
            int c = digits.charAt(i) - '2';
            if (c < 0) {
                continue;
            }
            array = re(chars[c], array);
        }
        return Arrays.asList(array);
    }

    public String[] re(char[] chars, String[] array) {
        String[] copy = new String[chars.length * array.length];
        int num = 0;
            for (String s : array) {
                for (char aChar : chars) {
                    copy[num] = s + aChar;
                    num++;
                }
            }
        return copy;
    }
}
